#include "SkyBoxShader.h"
#include "WRenderer.h"

void SkyBoxShader::Bind()
{
	if(sParamBlock != nullptr)
	{
		glm::mat4 viewMatrix = WRenderer::renderer->GetViewMatrix();
		glm::mat4 projectionMatrix = WRenderer::renderer->GetProjectionMatrix();

		//negate rotation so the environment rotates the other way round, no translation so it rotates around the camera
		glm::mat4 skyBoxViewMat = glm::mat4(-viewMatrix[0].x, -viewMatrix[0].y, -viewMatrix[0].z, 0.0f,
											-viewMatrix[1].x, -viewMatrix[1].y, -viewMatrix[1].z, 0.0f,
											-viewMatrix[2].x, -viewMatrix[2].y, -viewMatrix[2].z, 0.0f,
											0, 0, 0, 1.0f);

		sParams.viewProjMat = projectionMatrix * skyBoxViewMat;
		sParamBlock->Upload();

		glDisable(GL_CULL_FACE);
		glDisable(GL_DEPTH_TEST);
	}

	glUseProgram(this->programID);
	if(texParameters.count(Shader::Diffuse) > 0)
	{
		GLuint textureID = texParameters.at(Shader::Diffuse);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
	}
}


void SkyBoxShader::Unbind() 
{
	if(texParameters.count(Shader::Diffuse) > 0)
	{
		//std::shared_ptr<Texture> tex = texParameters.at(Shader::Diffuse);
		glActiveTexture(GL_TEXTURE0);
		//tex->Unbind();
		
		glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
	}
	glUseProgram(0);

	glEnable(GL_CULL_FACE);
	glEnable(GL_DEPTH_TEST);
}